summary.fun = function(data, varnames){
  summary = data %>% ungroup() %>%
    group_by(w_level_any_fac, quota.minority.cl, female) %>%
    summarize_at(vars(varnames), .funs = list(mean_nona, sd_nona, count_nonna)) %>%
    filter(!is.na(w_level_any_fac)) %>%
    filter(!is.na(female)) %>%
    rename(mean = fn1, sd = fn2, count = fn3) %>%
    mutate(se = sd/sqrt(count))
  return(summary)
}


gq = read_dta("data/global_quotas.dta")


# Create a dataset with female quotas only and minority quotas 
gq = apply(gq, M = 2, FUN = function(x){ifelse(x==""|x==".", NA, x)}) %>%
  as.data.frame() %>%
  filter(!is.na(Country))

gq = gq %>%
  # For countries where there is a range of quotas, assign higher level
  mutate(Country = as.character(Country),
         w_level_leg = w_level_leg %>% as.character(),
         w_level_cl = case_when(
           !grepl(w_level_leg, pattern = "-") ~ w_level_leg,
           w_level_leg=="20-50" ~ "50",
           w_level_leg=="30-50" ~ "50",
           w_level_leg=="10-25" ~ "25"), 
         w_level_cl = w_level_cl %>% as.numeric(),
         m_level_cl = ifelse(is.na(m_level), m_level_2, m_level) %>% destring(),
         w_level_cl = ifelse(is.na(w_level_cl), 0, w_level_cl),
         m_level_cl = ifelse(is.na(m_level_cl), 0, m_level_cl),
         w_level_any = ifelse(w_level_cl>0, 1, 0),
         m_level_any = ifelse(m_level_cl>0, 1, 0)) %>%
  mutate_at(vars(healthpercent_2009, healthpercent_2012, edupercent_2009,
                 edupercent_2012, milpercent_2009, milpercent_2012),
            .funs = destring)

width = 11.2
height = 8.22

ind.q = read.csv("data/individual_level_quotas.csv",
                 stringsAsFactors = F)

# Merge with whether female quotas exist
ind.q.j = ind.q %>%
  mutate(country_cl = country %>% trimws(),
         country_cl = case_when(
           country=="Cote d'ivoire" ~ "Ivory Coast",
           country=="Palestine" ~ "Palestinian Authority",
           country=="United States" ~ "United States of America",
           TRUE ~ country_cl)) %>%
  left_join(gq %>% dplyr::select(Country, w_level_cl, m_level_cl,
                                 w_level_any, m_level_any), 
            by = c("country" = "Country")) %>%
  filter(m_level_any==1) %>%
  mutate(quota.minority.cl = case_when(
    quota.minority == "0" ~ 0,
    quota.minority == "1" ~ 1,
    TRUE ~ 0), 
  female = case_when(
    V240 == "1" ~ 0,
    V240 == "2" ~ 1,
    TRUE ~ NA_real_), 
  victim_racism = case_when(
    V174 == "1" ~ 4,
    V174 == "2" ~ 3, 
    V174 == "3" ~ 2,
    V174 == "4" ~ 1,
    TRUE ~ NA_real_), 
  autonomy = ifelse(V55==-5, NA, V55), 
  w_level_any_fac = factor(w_level_any, labels = c("No W Quotas", "Women's Quotas")))

## Bar plots - "AUTONOMY"
auto.sum = ind.q.j %>% summary.fun(varnames = "autonomy") 
auto.pl = ggplot(auto.sum, aes(x = as.factor(quota.minority.cl), y = mean, group = as.factor(female)))+
  geom_bar(stat = "identity", aes(fill = as.factor(female)), 
           position = position_dodge(width = 1))+
  facet_grid(.~as.factor(w_level_any_fac))+
  scale_x_discrete(breaks = c(0,1), 
                   labels = c("Non-minority", "Quota minority"))+
  scale_fill_manual(values = c("gray", "firebrick4"), name = "",
                     labels = c("Male", "Female"))+
  labs(x = "", y = "Autonomy Over One's Life (1-10)")+
  theme_pubr()+
  theme(strip.text = element_text(size = 14))+
  theme(axis.text = element_text(size = 14))+
  theme(legend.text  = element_text(size = 14))+
  theme(axis.title = element_text(size = 14))
auto.pl



## Bar plots - "RACISM"
racism.sum = ind.q.j %>% summary.fun(varnames = "victim_racism") 
racism.pl = ggplot(racism.sum, aes(x = as.factor(quota.minority.cl), y = mean, group = as.factor(female)))+
  geom_bar(stat = "identity", aes(fill = as.factor(female)), 
           position = position_dodge(width = 1))+
  facet_grid(.~as.factor(w_level_any_fac))+
  scale_x_discrete(breaks = c(0,1), 
                   labels = c("Non-minority", "Quota minority"))+
  scale_fill_manual(values = c("gray", "firebrick4"), name = "",
                    labels = c("Male", "Female"))+
  labs(x = "", y = "Victim of Racist Behavior (1-4)")+
  theme_pubr()+
  theme(legend.position = "none")+
  theme(strip.text = element_text(size = 14))+
  theme(axis.text = element_text(size = 14))+
  theme(legend.text  = element_text(size = 14))+
  theme(axis.title = element_text(size = 14))+
  theme(strip.text = element_blank())
racism.pl


# Combine plots
ggarrange(auto.pl, racism.pl,  labels = NULL, common.legend = T, 
          nrow = 2)
ggsave(filename = paste0(fig.out, "Figure4.pdf")) 



